
<template>
  <div>
    <Modal
      class="tempFW"
      v-model="showBox"
      :title="$app.judgeData(editObj)?'编辑账户':'新增账户'"
      :mask-closable="false"
      @on-visible-change="vChange"
      :footer-hide="true"
      width="750"
    >
      <Row :gutter="30">
        <Col span="14">
          <div class="marginB20 f_16">基础信息</div>
          <Input
            v-model="mobile"
            placeholder="请输入手机号"
            type="number"
            onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"
            ref="mobile"
            class="marginB20 "
            size="large"
            :disabled="$app.judgeData(editObj)"
            clearable
          >
            <Icon type="ios-call" slot="prefix" />
          </Input>
          <Input
            v-model="nameT"
            placeholder="请输入昵称"
            ref="name"
            class="marginB20"
            size="large"
            clearable
          >
            <Icon type="ios-contact" slot="prefix" />
          </Input>
          <Input
            v-model="role_name"
            placeholder="请输入角色名（如：财务）"
            ref="role_name"
            class="marginB20"
            size="large"
            clearable
          >
            <Icon type="ios-people" slot="prefix" />
          </Input>
          <div v-show="$app.judgeData(editObj)">
            <Input
              v-model="password"
              placeholder="修改子账号密码（选填）"
              ref="password"
              type="password"
              class="marginB5"
              size="large"
              clearable
            >
              <Icon type="md-list" slot="prefix" />
            </Input>
            <div>
              <span class="cRed f_20">*</span>
              子账户的原密码将无效，必须使用该密码登录。
            </div>
          </div>
          
          <div  v-show="!$app.judgeData(editObj)">
            <Divider orientation="left" class="f_16 marginB10">温馨提示</Divider>
            <div class="padding0-10 lineH2">
              请子账户登录后，请关注服务猫公众号，以及绑定邮箱。<br/>
              否则无法接收系统推送或订单更新的消息。<br/>
              <div class="paddingT10 cRed">新增账户密码将以短信的形式发送到对应的注册手机上。</div>
            </div>
          </div>
          


        </Col>
        <Col span="10">
          <div class="marginB10 f_16">
            权限配置
            <Button type="primary" size="small" shape="circle" class="relative"
              ghost
              style="margin-left:20px; top:-2px;"
              @click="qZ"
            >全取消</Button>
            <Button type="primary" size="small" shape="circle" class="relative"
              style="margin-left:10px; top:-2px;"
              @click="qX"
            >全选</Button>
            <div class="pullRight cCCC pointer" @click="$app.alert('修改权限后，子账号需重新登录才能生效。')">
              <Icon type="ios-help-circle-outline" size="24"/>
            </div>
          </div>
          <Tree :data="navList" ref="tree" show-checkbox class="rootTree"></Tree>
        </Col>
      </Row>
      <Divider class="marginT10 marginB10" />
      <div class="overflowH">
        <Button type="primary" class="pullRight" @click="goFun">确 定</Button>
      </div>
    </Modal>
  </div>
</template>

<style scoped>
.rootTree {
  max-height: 500px;
  min-height: 300px;
  overflow: auto;
}
</style>

<script>
export default {
  name: "otherUser",
  components: {},
  data() {
    let data = {
      // 弹框是否显示
      showBox: false,
      mobile: "", //手机号
      nameT:"",//用户昵称
      role_name: "", //角色名称
    };

    let tool = {
      editObj:{},
      password:'',//强制修改密码
      navList: []//菜单权限
    };

    return Object.assign(data, tool);
  },
  created() {},
  mounted() {},
  methods: {
    //初始化密码
    inP(){
       $app.confirm('初始化密码？','账号：'+this.mobile,{
        yesFun:()=>{
          alert('还没有做')
        },
      });
    },

    //全选函数
    qX(){
      let {
        navList
      }=this;

      let oldList=[];

      navList.forEach((val,i)=>{

        let children=[];
        (val.children).forEach((yal,y)=>{
          children.push({
            title:yal.title,
            type:yal.type,
            checked:true
          });
        });
        
        oldList.push({
          title: val.title,
          expand: true,//展开子节点
          type:val.type,
          children:children,
          disabled:(val.type=='home'),
          checked:true
        });

      });

      this.navList=oldList;//赋值
    },

    //全取消
    qZ(){
      let {
        navList
      }=this;

      let oldList=[];

      navList.forEach((val,i)=>{
        

        let children=[];
        (val.children).forEach((yal,y)=>{
          children.push({
            title:yal.title,
            type:yal.type,
            checked:false
          });
        });
        
        oldList.push({
          title: val.title,
          expand: true,//展开子节点
          type:val.type,
          children:children,
          disabled:(val.type=='home'),
          checked:(val.type=='home')
        });

      });

      this.navList=oldList;//赋值
    },

    //编辑好，提交
    editBtn(okType){

      // console.log(okType);
      let that = this;
      let {
        password,
        editObj:{
          id:user_id
        },
        nameT, mobile,role_name 
      } = that;
      let youY=$app.delSpace(password);
      if ($app.judgeData(youY) && youY.length<6) {
        that.$Message.error({ content: "新密码不能低于6位数" });
        that.$refs["password"].focus();
      } else {
        $ajax.mEditUser(user_id,nameT,password,role_name,okType,()=>{
          this.initPp();
        });
      }
    },
    goFun() {

      let that = this;

      // 获取选中的节点
      let xZ=this.$refs['tree'].getCheckedNodes();
      let okType=[];
      //循环出来所需要的数据，只要子节点
      xZ.forEach((val,i)=>{
        // 只取，没有儿子，并且有type ，亦为根节点
        if(!$app.judgeData(val.children) && $app.judgeData(val.type) ){
          okType.push(val.type);
        }
      });

      let { nameT, mobile,role_name } = that;
      if (!$app.telCheck(mobile)) {
        that.$Message.error({ content: "请输入正确的手机号码，勿含空格等特殊字符" });
        that.$refs["mobile"].focus();
      } else if (!$app.judgeData($app.delSpace(nameT))) {
        that.$Message.error({ content: "昵称不能为空" });
        that.$refs["name"].focus();
      } else if (!$app.judgeData($app.delSpace(role_name))) {
        that.$Message.error({ content: "角色名称不能为空" });
        that.$refs["role_name"].focus();
      } else {

        if($app.judgeData(that.editObj)){//有值代表是编辑
          this.editBtn(okType);
        }else{
          $ajax.mAddUser(mobile,nameT,role_name,okType,()=>{
            this.initPp();
          },that);
        }

      }

    },
    initPp(){
      //执行父亲的方法，更新列表
      this.$parent.getList();
      this.showBox=false;//隐藏本页面弹框
    },
    //弹框消失
    vChange(buEr) {
      if (!buEr) {//关闭
        console.log('关闭弹框')
        setTimeout(()=>{//延迟体验更优秀

          //初始化所有
          this.mobile=''; //手机号
          this.nameT='';//用户昵称
          this.role_name=''; //角色名称
          this.editObj={};//初始化密码按钮
          this.navList=[];//菜单权限初始化

        },200);

      } else {//显示弹框

        let oldMenu=[];//
        let oldAdmin=true;//true管理员 false子账号
        // 编辑，资料填充
        if($app.judgeData(this.editObj)){
          let {
            editObj:{
              mobile,
              name,
              is_admin,
              role_name,
              menu_list
            }
          }=this;
          this.mobile=mobile;
          this.nameT=name;
          this.role_name=role_name;
          oldMenu=menu_list;
          oldAdmin=is_admin;
        }
        
        // 权限菜单
        ($app.leftNavRoot()).forEach((val,i)=>{

          let children=[];
          (val.children).forEach((yal,y)=>{
            let checked=false;
            // 编辑的时候，看看儿子谁选中了？
            oldMenu.forEach((jar,r)=>{
              if(jar==yal.type){
                checked=true;
              }
            });
            children.push({
              title:yal.title,
              type:yal.type,
              checked
            });
          });

          let checked=false;
          // 编辑的时候，看看儿子谁选中了？
          oldMenu.forEach((jar,r)=>{
            if(jar==val.type){
              checked=true;
            }
          });


          
          this.navList.push({
            title: val.title,
            expand: true,//展开子节点
            type:val.type,
            children:children,
            disabled:(val.type=='home'),
            checked:(val.type=='home')||checked
          });

        });

        // 管理员 即为新增账号
        if(!!oldAdmin){
          this.qX();
        }


      }
    }
  }
};
</script>
